package com.example.shangguigu.rsa_ecc;

import java.security.*;
import java.util.Base64;

/**
 * @DESCRIPTION: 生成RSA密钥对
 * @USER: shg
 * @DATE: 2023/2/25 16:23
 */
public class GenPriAndPub {

    public static void main(String[] args) throws Exception {
        getRsaPrivateAndPublicKey();
    }

    private static void getRsaPrivateAndPublicKey() throws Exception {

        // 创建密钥对
        KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance("RSA");
        pairGenerator.initialize(1024,new SecureRandom());
        KeyPair keyPair = pairGenerator.generateKeyPair();

        // 获取私钥，公钥
        PrivateKey aPrivate = keyPair.getPrivate();
        PublicKey aPublic = keyPair.getPublic();

        byte[] privateEncoded = aPrivate.getEncoded();
        byte[] publicEncoded = aPublic.getEncoded();

        String privateKey = Base64.getEncoder().encodeToString(privateEncoded);
        System.out.println(privateKey);

        String publicKey = Base64.getEncoder().encodeToString(publicEncoded);
        System.out.println(publicKey);

    }
}
